package com.bdqn.business.quality.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.business.access.dao.RoleRepository;
import com.bdqn.business.access.dao.UserRepository;
import com.bdqn.business.quality.dao.ClassesMapper;
import com.bdqn.business.quality.dao.StudentManagerMapper;
import com.bdqn.entity.access.Role;
import com.bdqn.entity.access.User;
import com.bdqn.entity.quality.Classes;
import com.bdqn.entity.quality.Student;
import com.bdqn.entity.quality.UserStudent;
import com.bdqn.response.Result;
import com.bdqn.response.ResultCode;
import com.bdqn.utils.IdWorker;
import com.bdqn.utils.PinYinUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class StudentManagerService {

    @Autowired
    private StudentManagerMapper studentManagerMapper;
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ClassesMapper classesMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RoleRepository roleRepository;

    public Result getStudentList() {
        List<Student> students = studentManagerMapper.selectList(null);

        List<UserStudent> list = new ArrayList<>();

        if(students != null && students.size() > 0){
            for (Student student : students) {
                UserStudent userStudent = new UserStudent();
                User user = userRepository.findById(student.getId()).get();
                BeanUtils.copyProperties(user,userStudent);
                BeanUtils.copyProperties(student,userStudent);
                list.add(userStudent);
            }
        }

        return new Result(ResultCode.SUCCESS,list);
    }

    public Result saveStudent(UserStudent userStudent) {



        User user = new User();
        Student student = new Student();

        BeanUtils.copyProperties(userStudent,user);
        BeanUtils.copyProperties(userStudent,student);

        String id = idWorker.nextId() + "";
        user.setId(id);
        String username = PinYinUtil.toPinyin(user.getNickName()) + (int)(Math.random()*8999);
        user.setUsername(username);
        user.setLevel("user");

        user.setStatus("1");
        user.setPassword(new Md5Hash("123456",username,3).toString()); //参数一: 加密谁?   混淆字符串1234 : 加盐  3 加密次数


        //处理用户与角色的关联关系
        Set<Role> roles = new HashSet<>();
        for (String roleId : user.getRoleIds().split(",")) {
            Role role = roleRepository.findById(roleId).get();
            roles.add(role);
        }

        if(roles != null && roles.size() > 0){
            user.setRoles(roles);
            userRepository.save(user);


            //String classesId = student.getClassesId();
            Classes classes = classesMapper.selectById(userStudent.getClassesId());
            student.setClassesName(classes.getClassesName());

            student.setStudyType("1"); //1 在读  2休学 3 退学 4毕业

            student.setId(id);
            studentManagerMapper.insert(student);

            //更新班级人数
            classes.setPersonNumber(classes.getPersonNumber() + 1);

            classesMapper.updateById(classes);

            return Result.SUCCESS();
        }else{
            return Result.FAIL();
        }
    }
}
